package org.jaaksi.customviewdemo

import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.layout_gender_select.view.*
import kotlin.math.abs

class GenderSelectView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {

    companion object {
        const val SCALE = 0.8f

        //初始状态
        const val STATUS_INIT = 0

        //选择男性状态
        const val STATUS_MALE = 1

        //选择女性状态
        const val STATUS_FEMALE = 2

    }

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_gender_select, this)
    }

    private var maleWith = 0
    private var initOffset = 0f
    private var revertOffset = 0f
    private var selectedOffset = 0f
    private var isMove = false
    private var move = 0f
    private var lastX = 0f
    private var status = STATUS_INIT

    // 最小滑动距离
    private val scaledTouchSlop = ViewConfiguration.get(getContext()).scaledTouchSlop

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        maleWith = view_male.measuredWidth
        revertOffset = maleWith * SCALE / 2.0f
        initOffset = (w / 2 - maleWith) / 2.0f
        selectedOffset = (w - maleWith) / 2.0f
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                lastX = event.x
                isMove = false
            }
            MotionEvent.ACTION_MOVE -> {
                isMove = abs(event.x - lastX) >= scaledTouchSlop
                if (isMove) {
                    move = event.x - lastX
                }
            }
            MotionEvent.ACTION_UP -> {
                if (!isMove) {
                    // 判断是否在view区域内
                    if (isViewClick(view_male, event)) {
                        if (status != STATUS_MALE){
                            // 直接设置move为最大距离，然后scroll的时候再根据当前状态判断
                            move = selectedOffset - revertOffset
                        }
                        /*if (status == STATUS_INIT) {
                            move = selectedOffset - initOffset
                        } else if (status == STATUS_FEMALE) {
                            move = selectedOffset - revertOffset
                        }*/ else {
                            return true
                        }
                    }
                    if (isViewClick(view_female, event)) {
                        if (status == STATUS_INIT) {
                            move = selectedOffset - initOffset
                        } else if (status == STATUS_MALE) {
                            move = selectedOffset - revertOffset
                        } else {
                            return true
                        }
                    }
                }
                scrollToEnd()
            }
        }

        return true
    }

    // 手指抬起之后的动画
    private fun scrollToEnd() {
        view_male.animate().cancel()
        when (status) {
            STATUS_INIT -> {
                when {
                    move > 0 && abs(move) >= selectedOffset - initOffset -> {
                        status = STATUS_MALE
                        animMaleFromInitToSelected()
                    }
                }
            }
        }
    }

    // 男性从init态到选中态
    private fun animMaleFromInitToSelected() {
        view_male.animate()
            .scaleX(1f)
            .scaleY(1f)
            .alpha(1f)
            .translationX(selectedOffset - initOffset)
            .setDuration(500)
            .start()
        view_female.animate()
            .scaleX(SCALE)
            .scaleY(SCALE)
            .alpha(0.5f)
            .translationX(selectedOffset - revertOffset)
            .setDuration(500)
            .start()
    }

    private fun animMaleFromSelectedToRevert() {
        view_male.animate()
            .scaleX(SCALE)
            .scaleY(SCALE)
            .alpha(0.5f)
            .translationX(revertOffset - selectedOffset)
            .setDuration(500)
            .start()
        view_female.animate()
            .scaleX(1f)
            .scaleY(1f)
            .alpha(1f)
            .translationX(revertOffset - selectedOffset)
            .setDuration(500)
            .start()
    }

    private fun isViewClick(view: View, event: MotionEvent?): Boolean {
        if (event == null) {
            return false
        }
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        val left = location[0]
        val top = location[1]
        val right = left + view.measuredWidth
        val bottom = top + view.measuredHeight
        val x = event.rawX
        val y = event.rawY
        if (y >= top && y <= bottom && x >= left
            && x <= right
        ) {
            return true;
        }
        return false
    }
}